package com.huang825172.sbog.Tabs.Create.CapturePage.View;

import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.camerakit.CameraKit;
import com.camerakit.CameraKitView;
import com.huang825172.sbog.R;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CapturePage extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.CapturePageCameraKitView)
    CameraKitView cameraKitView;

    @BindView(R.id.CapturePageCaptureButton)
    ImageView captureButtonImageView;

    private boolean pendingVideoCapture = false;
    private boolean capturingVideo = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capture_page);

        ButterKnife.bind(this);

        captureButtonImageView.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        cameraKitView.onStart();
    }

    @Override
    protected void onStop() {
        cameraKitView.onStop();
        super.onStop();
    }

    @Override
    protected void onPause() {
        cameraKitView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        cameraKitView.onResume();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        cameraKitView.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.CapturePageCaptureButton: {
                if (!capturingVideo) {
                    pendingVideoCapture = true;
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (pendingVideoCapture) {
                                capturingVideo = true;
                                pendingVideoCapture = false;
                                cameraKitView.captureVideo(new CameraKitView.VideoCallback() {
                                    @Override
                                    public void onVideo(CameraKitView cameraKitView, Object o) {
                                        Toast.makeText(getApplicationContext(), "videoDone" + o.toString(), Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }
                    }, 250);
                } else {
                    capturingVideo = false;
                    cameraKitView.stopVideo();
                }
                break;
            }
            default:
                break;
        }
    }
}
